package nio.common;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class ServerSocketChannelDemo2 {
	public static void main(String[] args) {
		createServerChannel();
	}

	public static void createServerChannel() {
		try {
			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.socket().bind(new InetSocketAddress("127.0.0.1", 9999));
			while (true) {
				System.out.println("=====");
//				serverSocketChannel.configureBlocking(false);
				SocketChannel socketChannel = serverSocketChannel.accept();
				ByteBuffer buf = ByteBuffer.allocate(5);
				buf.clear();
				buf.put("secondServersocketChannel".getBytes());
				buf.flip();
				socketChannel.write(buf);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
